package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import com.sky.vo.DishVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Resource(name = "shoppingCartMapper")
    ShoppingCartMapper shoppingCartMapper;
    @Resource(name = "dishMapper")
    DishMapper dishMapper;
    @Resource(name = "setmealDishMapper")
    SetmealDishMapper setmealDishMapper;

    /**
     * 添加购物车商品。
     *
     * @param shoppingCartDTO 购物车数据传输对象，包含用户选择的商品或套餐信息。
     *                        包括商品ID、套餐ID、数量等信息，但不包含用户ID，用户ID由方法内部设置。
     *                        <p>
     *                        方法首先会尝试根据用户ID、商品ID（或套餐ID）和口味查询购物车表中是否已有相同项。
     *                        如果已存在，则在原有数量上加1并更新修改时间；如果不存在，则根据传入的dishId或setmealId查询商品或套餐信息，
     *                        并插入新的购物车记录。
     *                        整个操作在事务中执行，任何异常都会导致事务回滚。
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(ShoppingCartDTO shoppingCartDTO) {
        // 初始化购物车对象并设置用户ID
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setUserId(BaseContext.getCurrentId());
        // 使用DTO信息填充购物车对象
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        // 查询购物车表中是否存在相同项
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.list(shoppingCart);
        if (!shoppingCartList.isEmpty()) {
            // 如果存在相同项，则在原数量上加1并更新修改时间
            shoppingCart = shoppingCartList.get(0);
            shoppingCart.setNumber(shoppingCart.getNumber() + 1);
            shoppingCartMapper.updateTime(shoppingCart);
        } else {
            // 如果不存在相同项，根据dishId或setmealId查询商品或套餐信息，并插入新的购物车记录
            if (shoppingCartDTO.getDishId() != null) {
                DishVO dishVO = dishMapper.selDishById(shoppingCart.getDishId());
                shoppingCart.setImage(dishVO.getImage());
                shoppingCart.setName(dishVO.getName());
                shoppingCart.setAmount(dishVO.getPrice());
            } else {
                SetmealVO setmealVO = setmealDishMapper.selSetmealById(shoppingCart.getSetmealId());
                shoppingCart.setName(setmealVO.getName());
                shoppingCart.setImage(setmealVO.getImage());
                shoppingCart.setAmount(setmealVO.getPrice());
            }
            // 设置创建时间和数量为1，并插入购物车表
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCart.setNumber(1);
            shoppingCartMapper.add(shoppingCart);
        }

    }

    //查看购物车
    // TODO 还需要解决购物车中的商品在商家对商品信息做出修改时应该怎么维护购物车表以及查询量大如何缓存该表信息
    @Override
    public List<ShoppingCart> selShoppingCart() {
        ShoppingCart shoppingCart = ShoppingCart.builder()
                .userId(BaseContext.getCurrentId())
                .build();
        return shoppingCartMapper.list(shoppingCart);
    }

    //删除购物车中一个商品
    @Override
    public void delShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        shoppingCartMapper.delShoppingCart(shoppingCart);
    }

    //清空购物车
    @Override
    public void cleanShoppingCart() {
        ShoppingCart shoppingCart = ShoppingCart.builder()
                .userId(BaseContext.getCurrentId())
                .build();
        shoppingCartMapper.delShoppingCart(shoppingCart);
    }


}
